package utils

import (
	"strings"
	"strconv"
	"encoding/json"
	"github.com/liu-junyong/go-logger/logger"
)

func Str2Int32Arr(str string) []int32 {
	if str[0] == '[' && str[len(str)-1] == ']' {
		arr := strings.Split(str[1:len(str)-1], ",")
		ret := make([]int32, 0, len(arr))
		for i := 0; i < len(arr); i++ {
			if val, ok := strconv.Atoi(arr[i]); ok == nil {
				ret = append(ret, int32(val))
			} else {
				return []int32{}
			}
		}
		return ret
	}
	return []int32{}
}

func Str2MapIntInt(str string) map[int]int {
	defer func() {
		if r := recover(); r != nil {
			logger.Error(r)
		}
	}()
	ret := make(map[int]int)
	json.Unmarshal([]byte(str),&ret)
	return ret
}
